// exokernel API接口定义（通过FFI调用）
// 这是唯一包含unsafe代码的文件

use super::super::core::types::{CapabilityId, PhysicalAddress, VirtualAddress, MemoryPermissions};

// Exokernel核心API
#[link(name = "exokernel")]
extern "C" {
    // 能力管理API
    pub fn exokernel_get_capability(cap_id: CapabilityId) -> bool;
    pub fn exokernel_release_capability(cap_id: CapabilityId);
    
    // 内存管理API
    pub fn exokernel_allocate_physical_memory(
        cap_id: CapabilityId,
        size: u64,
        alignment: u64,
    ) -> PhysicalAddress;
    pub fn exokernel_free_physical_memory(
        cap_id: CapabilityId,
        addr: PhysicalAddress,
        size: u64,
    ) -> bool;
    pub fn exokernel_map_memory(
        cap_id: CapabilityId,
        virt_addr: VirtualAddress,
        phys_addr: PhysicalAddress,
        size: u64,
        perms: MemoryPermissions,
    ) -> bool;
    pub fn exokernel_unmap_memory(
        cap_id: CapabilityId,
        virt_addr: VirtualAddress,
        size: u64,
    ) -> bool;
    
    // 中断处理API
    pub fn exokernel_set_interrupt_handler(
        cap_id: CapabilityId,
        irq: u32,
        handler: u64,
    ) -> bool;
    pub fn exokernel_trigger_interrupt(
        cap_id: CapabilityId,
        irq: u32,
    ) -> bool;
    
    // CPU时间片管理API
    pub fn exokernel_get_cpu_time_slice(
        cap_id: CapabilityId,
        milliseconds: u32,
    ) -> bool;
    
    // IO端口操作API
    pub fn exokernel_inb(cap_id: CapabilityId, port: u16) -> u8;
    pub fn exokernel_outb(cap_id: CapabilityId, port: u16, value: u8);
    
    // 初始能力获取API
    pub fn exokernel_get_initial_capabilities(
        out_mem_cap: *mut CapabilityId,
        out_cpu_cap: *mut CapabilityId,
    ) -> bool;
}

// 驱动管理器API
#[link(name = "exokernel")]
extern "C" {
    pub fn exokernel_driver_manager_init(memory_manager_ptr: *mut u8, capability_manager_ptr: *mut u8, trap_handler_ptr: *mut u8) -> bool;
    pub fn exokernel_driver_manager_deinit();
    pub fn exokernel_driver_load(driver_ptr: *mut u8) -> bool;
    pub fn exokernel_driver_unload(driver_ptr: *mut u8) -> bool;
    pub fn exokernel_device_register(device_info_ptr: *mut u8) -> u64;
    pub fn exokernel_device_remove(device_id: u64);
    pub fn exokernel_device_ioctl(device_id: u64, cmd: u32, data_ptr: *mut u8, size: u64) -> isize;
    pub fn exokernel_driver_register_irq_handler(cap_id: CapabilityId, irq: u32, handler: u64) -> bool;
    pub fn exokernel_driver_unregister_irq_handler(cap_id: CapabilityId, irq: u32) -> bool;
    pub fn exokernel_driver_send_eoi(cap_id: CapabilityId, irq: u32);
}